package com.bsb.biz.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.bsb.base.common.config.Global;
import com.bsb.base.common.util.DateUtils;
import com.bsb.base.common.util.StringUtils;
import com.bsb.base.model.BsbUser;
import com.bsb.base.model.BsbUserExample;
import com.bsb.base.model.SysUser;
import com.bsb.base.model.BsbUserExample.Criteria;
import com.bsb.base.model.UserFirm;
import com.bsb.biz.service.BsbUserService;
import com.bsb.biz.shiro.UserUtils;

@Controller
public class BsbUserController{
	
	private int defaultPageSize=10;
	
	private String redirect = "redirect:/bsbUser/list.do";
	
	@Resource
	private BsbUserService bsbUserService;
	@RequiresPermissions("bsbUser:view")
	@RequestMapping(value="/bsbUser/list",method=RequestMethod.GET)
	public String list(
			Model model,@RequestParam Map<String, Object> params,
			@RequestParam(value = "pageNum", required = false) Integer pageNum,
			@RequestParam(value = "pageSize", required = false) Integer pageSize) {
		int maxPageNum = 0;
		if (pageSize == null) {
			pageSize = defaultPageSize;
		}
		SysUser currentUser = UserUtils.currentUser();
		Long firmId = currentUser.getFirmId();
		if(firmId==null){
			model.addAttribute("msg", "账号异常请联系管理员！");
			return "/BsbUser/list";
		}
		BsbUserExample bsbUserExample = new BsbUserExample();
		Criteria createCriteria = bsbUserExample.createCriteria();
		
		String name = (String) params.get("name");
		String phone = (String) params.get("phone");
		String vip = (String) params.get("vip");
		String firm = (String) params.get("firm");
		String start = (String) params.get("start");
		Date startDate = DateUtils.parseDate(start);
		String end = (String) params.get("end");
		Date endDate = DateUtils.parseDate(end);
		
		String firmName=null;
		if(StringUtils.isNotBlank(firm)){
			firmName="%"+firm+"%";
		}
		createCriteria.andRoleBetween("0", "1");
		if(StringUtils.isNotBlank(phone)){
			createCriteria.andPhoneLike("%"+phone+"%");
		}
		if(StringUtils.isNotBlank(name)){
			createCriteria.andNameLike("%"+name+"%");
		}
		if(StringUtils.isNotBlank(vip)){
			createCriteria.andVipEqualTo(vip);
		}
		
		if(startDate!=null){
			createCriteria.andRegistTimeGreaterThanOrEqualTo(startDate);
		}
		if(endDate!=null){
			endDate=DateUtils.addDays(endDate, 1);
			createCriteria.andRegistTimeLessThanOrEqualTo(endDate);
		}
		
		if(firmId!=10000){
			createCriteria.andFirmIdEqualTo(firmId.intValue());
		}
		createCriteria.andDelFlagEqualTo(Global.SHOW);
		int totalCount = bsbUserService.getCountUserFirm(firmName, bsbUserExample);
		//int totalCount = bsbUserService.countBsbUser(bsbUserExample);
		if(totalCount%pageSize == 0){
			maxPageNum = totalCount/pageSize;
		}else{
			maxPageNum = totalCount/pageSize+1;
		}
		if (pageNum == null || pageNum < 1) {
			pageNum = 1;
		}else {
			if(maxPageNum > 0 && pageNum > maxPageNum) {
				pageNum = maxPageNum;
			}
		}
		bsbUserExample.setPageIndex((pageNum - 1) * pageSize);
		bsbUserExample.setPageSize(pageSize);
		bsbUserExample.setOrderByClause("bsb_user.registTime desc");
		List<UserFirm> userFirms = bsbUserService.getUserFirms(firmName, bsbUserExample);
		model.addAttribute("name", name);
		model.addAttribute("phone", phone);
		model.addAttribute("vip", vip);
		model.addAttribute("firm", firm);
		model.addAttribute("firmId", firmId);
		model.addAttribute("start", start);
		model.addAttribute("end", end);
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("pageNum", pageNum);
		model.addAttribute("pageSize", pageSize);
		if(totalCount%pageSize == 0){
			model.addAttribute("maxPageNum", totalCount/pageSize);
		}else{
			model.addAttribute("maxPageNum", totalCount/pageSize+1);
		}
		model.addAttribute("bsbUsers", userFirms);
		return "/BsbUser/list";
	}
	
	@RequestMapping("/bsbUser/toAdd")
	public String toAdd(Model model) {
		return "/BsbUser/add";
	}

	@RequestMapping("/bsbUser/add")
	public String add(Model model, @ModelAttribute("bsbUser") BsbUser bsbUser) {
		bsbUserService.addBsbUser(bsbUser);
		return redirect;
	}

	@RequestMapping("/bsbUser/{uuid}/del")
	public String del(Model model,HttpServletRequest request,RedirectAttributes redirectAttributes,
			@PathVariable(value="uuid", required = false) String uuid) {
		String queryString = request.getQueryString();
		if(StringUtils.isEmpty(queryString)){
			queryString = "";
		}
		if(StringUtils.isNotBlank(uuid)){
			BsbUser BsbUser = bsbUserService.getBsbUserByUuid(uuid);
			if(BsbUser!=null){
				SysUser currentUser = UserUtils.currentUser();
				Long firm = currentUser.getFirmId();
				Integer firmId = BsbUser.getFirmId();
				if(firm==null||firmId==null){
					redirectAttributes.addFlashAttribute("msg", "账号异常请联系管理员！");
					return redirect + "?" +queryString;
				}
				if(firm!=10000){
					if(firm.intValue()!=firmId){
						//失败
						redirectAttributes.addFlashAttribute("msg", "失败 ！");
						return redirect + "?" +queryString;
					}
				}
				BsbUser bsbUser = new BsbUser();
				bsbUser.setUuid(uuid);
				bsbUser.setDelFlag(Global.HIDE);
				bsbUserService.updateBsbUser(bsbUser);
				redirectAttributes.addFlashAttribute("msg", "删除成功");
			}else {
				redirectAttributes.addFlashAttribute("msg", "失败 ！");
			}
			
		}else {
			redirectAttributes.addFlashAttribute("msg", "失败 ！");
		}
		return redirect + "?" +queryString;
	}

	@RequestMapping("/bsbUser/toEdit")
	public String toEdit(Model model,
			@RequestParam(value = "uuid", required = true) String uuid) {
		BsbUser bsbUser = bsbUserService.getBsbUserByUuid(uuid);
		model.addAttribute("bsbUser", bsbUser);
		return "/BsbUser/edit";
	}

	@RequestMapping("/bsbUser/edit")
	public String edit(Model model, @ModelAttribute("bsbUser") BsbUser bsbUser) {
		bsbUserService.updateBsbUser(bsbUser);
		return redirect;
	}

	@RequestMapping("/bsbUser/{uuid}/view")
	public String view(Model model,@PathVariable(value="uuid", required = false) String uuid) {
		UserFirm userFirm = bsbUserService.getUserFirmByUuid(uuid);
		model.addAttribute("bsbUser", userFirm);
		return "/BsbUser/view";
	}
}
